package com.gather.spring.retry.springtemp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.RecoveryCallback;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

/**
 * @author kxd
 * @date 2018/12/4 20:15
 * description:
 */
public class SimpleRetryDemo {
    private static final Logger log = LoggerFactory.getLogger(SimpleRetryDemo.class);

    public static void main(String[] args) throws Throwable {
        //spring策略模板
        RetryTemplate template = new RetryTemplate();
        //简单策略
        SimpleRetryPolicy policy = new SimpleRetryPolicy();
        policy.setMaxAttempts(3);
        //模板设置策略信息
        template.setRetryPolicy(policy);
        String result = template.execute(
                new RetryCallback<String, Throwable>() {
                    @Override
                    public String doWithRetry(RetryContext context) throws Throwable {
                        throw new NullPointerException();
                    }
                },
                new RecoveryCallback<String>() {
                    @Override
                    public String recover(RetryContext context) throws Exception {
                        return "recovery callback";
                    }
                }
        );
        log.info("result:{}", result);
    }
}
